package com.cn.wanxi.servlet.nav;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.service.NavService;
import com.cn.wanxi.service.impl.NavServiceImpl;
import com.cn.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 更新用post，查询用get
 */
@WebServlet("/back/nav/add")
public class NavAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        List<NavModel>  list=navService.getNavModelList();
//        String page = req.getParameter("page");
//        String limit = req.getParameter("limit");
//        2.拿到前端的数据
        String name = req.getParameter("name");
        String href = req.getParameter("href");
        String enable = req.getParameter("enable");
//3.封装model
        NavModel navModel = new NavModel();
        navModel.setName(name);
        navModel.setTitle(name);
//        navModel.setPage(Tool.strToPage(page));
//        navModel.setLimit(Tool.strToLimit(limit));
        navModel.setHref(href);
        navModel.setEnable(Tool.strToInt(enable));
//4.调用逻辑层
        NavService navService = new NavServiceImpl();
//        5.得到固定格式的返回值
       ResultModel resultModel  = navService.add(navModel);
//       6.返回给前端
        resp.getWriter().println(JSONObject.toJSONString(resultModel));
// 以后参数最好全部传递对象
//        List<NavModel> list = navService.getNavModelList(navModel, pageModel);

//        int count = navService.add(navModel);
    }


}
